package ltd.hxya.novel.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import ltd.hxya.novel.common.bean.Result;
import ltd.hxya.novel.common.bean.UserBean;
import ltd.hxya.novel.common.exception.NovelGlobalException;
import ltd.hxya.novel.common.to.book.BookTo;
import ltd.hxya.novel.common.bean.ResultEnum;
import ltd.hxya.novel.user.entity.UserBookshelf;
import ltd.hxya.novel.user.feign.BookFeignService;
import ltd.hxya.novel.user.interceptor.UserLoginInterceptor;
import ltd.hxya.novel.user.mapper.UserBookshelfMapper;
import ltd.hxya.novel.user.service.IUserBookshelfService;
import ltd.hxya.novel.user.vo.UserBookshelfVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户书架表 服务实现类
 * </p>
 *
 * @author hxya
 * @since 2022-09-13
 */
@Service
public class UserBookshelfServiceImpl extends ServiceImpl<UserBookshelfMapper, UserBookshelf> implements IUserBookshelfService {


    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private BookFeignService bookFeignService;
    /**
     * 根据用户id，查询出当前用户的书架上的书，并调用远程服务，查询出书的信息
     * @return
     */
    @Override
    public List<UserBookshelfVo> listBookByUserId() {
        //从localThread中取出 当前登录的用户
        UserBean userBean = UserLoginInterceptor.threadLocal.get();
        String userId = userBean.getId();
        //从表中查询最后阅读的目录
        QueryWrapper<UserBookshelf> userBookshelfQueryWrapper = new QueryWrapper<>();
        userBookshelfQueryWrapper.eq("user_id",userId);
        //从数据库中查询相关的信息
        List<UserBookshelf> userBookshelves = this.baseMapper.selectList(userBookshelfQueryWrapper);
        //遍历userBookshelves，将id封装成集合
        if (CollectionUtils.isEmpty(userBookshelves)){
            throw new NovelGlobalException(ResultEnum.NOT_DATA.getCode(), ResultEnum.NOT_DATA.getMsg());
        }
        Map<String,UserBookshelf> userBookshelfMap = new HashMap<>();
        List<String> bookIdList = userBookshelves.stream().map(userBookshelf -> {
            String bookId = userBookshelf.getBookId();
            userBookshelfMap.put(bookId,userBookshelf);
            return bookId;
        }).collect(Collectors.toList());

        Result<List<BookTo>> result = bookFeignService.bookListBatchId(bookIdList);
        if (result.getCode()!=200){
            throw new NovelGlobalException(result.getCode(),result.getMsg());
        }
        //判断查询到的BookTO是否为空，如果为空，则抛出未知异常
        List<BookTo> bookTos = result.getData();
        if (CollectionUtils.isEmpty(bookTos)){
            throw new NovelGlobalException(ResultEnum.UN_KNOWN_EXCEPTION.getCode(), ResultEnum.UN_KNOWN_EXCEPTION.getMsg());
        }
        //将查询得到的数据封装成Vo，返回给前端
        List<UserBookshelfVo> userBookshelfVos = bookTos.stream().map(bookTo -> {
            UserBookshelfVo userBookshelfVo = new UserBookshelfVo();
            UserBookshelf userBookshelf = userBookshelfMap.get(bookTo.getId());
            BeanUtils.copyProperties(bookTo,userBookshelfVo);
            BeanUtils.copyProperties(userBookshelf, userBookshelfVo);
            userBookshelf.setBookId(bookTo.getId());
            return userBookshelfVo;
        }).collect(Collectors.toList());
        return userBookshelfVos;
    }
}
